package com.hoshiibuy.sku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.sku.entity.ExtSku;
import com.hoshiibuy.sku.entity.param.ExtSkuQueryParam;
import com.hoshiibuy.sku.entity.param.ExtSkuUpdateParam;
import com.hoshiibuy.sku.entity.param.ExtSkuAddParam;
import com.hoshiibuy.sku.service.ExtSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 平台商品表SKU 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-05-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/sku/ext-sku")
@Api(value = "SKU管理", tags = {"平台商品表SKU管理"})
public class ExtSkuController {
    @Autowired
    private ExtSkuService extSkuService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询平台商品表SKU
    */
    @ApiOperation(value = "分页查询平台商品表SKU")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid ExtSkuQueryParam param) {
        try {
            log.info("分页查询平台商品表SKU");
            QueryWrapper<ExtSku> queryWrapper = new QueryWrapper<ExtSku>();
            queryWrapper.eq(param.getSpuid() != null, "spuid", param.getSpuid());
            queryWrapper.eq(param.getSkuid() != null, "skuid", param.getSkuid());
            queryWrapper.eq(StringUtils.isNotBlank(param.getSkuTitle()), "sku_title", param.getSkuTitle());
            queryWrapper.eq(param.getSalePrice() != null, "sale_price", param.getSalePrice());
            queryWrapper.eq(param.getMaketPrice() != null, "maket_price", param.getMaketPrice());
            queryWrapper.eq(param.getCostPrice() != null, "cost_price", param.getCostPrice());
            queryWrapper.eq(param.getVendorId() != null, "vendor_id", param.getVendorId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getSkuImage()), "sku_image", param.getSkuImage());
            queryWrapper.eq(StringUtils.isNotBlank(param.getExtendProperty()), "extend_property", param.getExtendProperty());
            queryWrapper.eq(StringUtils.isNotBlank(param.getExtendSpec()), "extend_spec", param.getExtendSpec());
            queryWrapper.eq(param.getPoPolicy() != null, "po_policy", param.getPoPolicy());
            queryWrapper.eq(param.getDispatchPolicy() != null, "dispatch_policy", param.getDispatchPolicy());
            queryWrapper.eq(param.getStatus() != null, "status", param.getStatus());
            queryWrapper.eq(param.getDeleted() != null, "deleted", param.getDeleted());
            IPage<ExtSku> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(ExtSku.class));
            return Response.data(extSkuService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询平台商品表SKU异常", e);
            return Response.failed("分页查询平台商品表SKU异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询平台商品表SKU
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询平台商品表SKU")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(extSkuService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台商品表SKU")
    public Response save(@RequestBody @Valid ExtSkuAddParam param) {
        try {
            ExtSku data = BeanUtils.covertType(param, ExtSku.class);
            return Response.data(extSkuService.save(data));
        } catch (ServiceException e) {
            log.error("新增平台商品表SKU异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增平台商品表SKU异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改平台商品表SKU
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台商品表SKU")
    public Response update(@RequestBody @Valid ExtSkuUpdateParam param) {
        try {
            ExtSku data = BeanUtils.covertType(param, ExtSku.class);
            return Response.data(extSkuService.updateById(data));
        }catch (ServiceException e) {
            log.error("修改平台商品表SKU异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改平台商品表SKU异常", e);
            return Response.failed();
        }
    }
}
